import { fetchCateList } from '@/api/product'

const state = {
  catelist: []
}

const mutations = {
  SET_CATE_LIST: (state, list) => {
    state.catelist = list
  }
}

const actions = {
  getCateList({ commit }) {
    return new Promise((resolve, reject) => {
      fetchCateList().then(response => {
        const list = response.categories
        const data = []
        list.forEach(item => {
          const tmp = []
          item.children.forEach(son => {
            tmp.push({
              value: son.id,
              label: son.name
            })
          })
          data.push({
            value: item.id,
            label: item.name,
            children: tmp
          })
        })
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
